VB Sample Code for Custom Print Link

Private Sub mnuPrintCustomPrint_Click()
Dim result%, jobnum%, mainjob%, HasSavedData%, dialogflag%, resultlong&
Dim JobInfo As PEJobInfo, TempText$
Dim PrintData As Printer
Dim curprinter%, defprinterpos%
Dim Mode As crDEVMODE

result% = PEOpenEngine()
If result% = 0 Then
MsgBox "Could not start the report engine. Execution must halt.",
vbOKOnly + vbCritical, "Serious Error"
End
End If

' Define size of JobInfo structure
JobInfo.StructSize = PE_SIZEOF_JOB_INFO

jobnum% = PEOpenPrintJob(lblReportName.Caption)
' Name from label on sample form
ErrorTrap "OpenPrintJob in PrintCustom", jobnum%

' Subreport check - if a subreport is currently selected on the main form,
' jobnum% becomes the subreport
If lblSubreportName.Visible Then
mainjob% = jobnum%
jobnum% = PEOpenSubreport(mainjob%, lblSubreportName.Caption)
ErrorTrap "OpenSubReport in PrintCustom", mainjob%
End If

' If this is the pro edition, do the SavedData test
result% = PEHasSavedData(jobnum%, HasSavedData%)
ErrorTrap "HasSavedData in PrintCustom", jobnum%
' If there is saved data, give the user the option of using or
' discarding it
If HasSavedData% <> 0 Then
If MsgBox("There is data saved with this report. Would you like to _
refresh from the original data? Press Yes to refresh, No to use the _
saved data.", vbYesNo + vbQuestion, "Refresh Data?") = vbYes Then
result% = PEDiscardSavedData(jobnum%)
ErrorTrap "DiscardSavedData in PrintCustom", jobnum%
End If
End If

If MsgBox("Do you want to see the progress dialog?", vbYesNo + vbQuestion,
"Display Progress Dialog?") = vbYes Then dialogflag% = True Else dialogflag%
= False
result% = PEEnableProgressDialog(jobnum%, dialogflag%) ' Set whether or not
to display progress dialog
ErrorTrap "EnabledProgressDialog in PrintCustom", jobnum%

result% = PESetDialogParentWindow(jobnum%, Sample.hWnd) ' Set dialog parent
to main form
ErrorTrap "SetDialogParentWindow in PrintCustom", jobnum%

result% = PEOutputToPrinter(jobnum%, 1) ' Second value is number of copies
ErrorTrap "OutputtoPrinter in PrintCustom", jobnum%

' Selecting a printer using crPESelectPrinter
' crPESelectPrinter requires the printer, driver and port names for the print
device you wish to ' send a report to. This information is available in the
printers collection of VB4.
' The list of printers available on the system is displayed 1
' Modally on the SelectPrint form, along with an option to change the
orientation of the print job.
' The orientation information is stored and passed in acrDEVMODE structure.
crDEVMODE structures can adjust the paper source, paper size and a host of other features.

Load SelectPrint
CenterForm Sample, SelectPrint

' Loop through printers collection to load selectprint form with
printer names
For Each PrintData In Printers
' Add printer name and port to list
SelectPrint!lstPrintList.AddItem PrintData.DeviceName & " at " &
PrintData.Port
' Check for default printer
If PrintData.DeviceName = Printer.DeviceName Then defprinterpos% =
SelectPrint!lstPrintList.NewIndex
Next
' The list box on the SelectPrint form is positioned to the default printer
and then displayed 1 ' Modally
SelectPrint!lstPrintList.ListIndex = defprinterpos%
SelectPrint.Show 1 ' Modal

' When execution returns, one of the buttons on the SelectPrint form has been
pressed. The Select Case statement determines which
Select Case SelectPrint.Tag
Case "Print"
' Get which printer was selected from the list
curprinter% = SelectPrint!lstPrintList.ListIndex
' Set crDEVMODE to whichever orientation was selected on the form
If SelectPrint!optPortrait.Value = True Then
mode.dmOrientation = DMORIENT_PORTRAIT
Else
mode.dmOrientation = DMORIENT_LANDSCAPE
End If
' NOTE - the dmFields element of the crDEVMODE structure must contain
what elements of the crDEVMODE
' structure are being used. To set multiple elements, use the OR
statement to combine the elements together
' In this example, only the orientation element is being set.
mode.dmFields = DM_ORIENTATION
' Do the SelectPrinter call
result% = crPESelectPrinter(jobnum%, Printers(curprinter%).DriverName,
Printers(curprinter%).DeviceName, Printers(curprinter%).Port,
mode)
ErrorTrap "SelectPrinter in PrintCustom", jobnum%
' Print the job
result% = PEStartPrintJob(jobnum%, True)
' Note that flag must ALWAYS be true - setting to false will yield
unpredictable results
ErrorTrap "StartPrintJob in PrintCustom", jobnum%
Case "Cancel"
MsgBox "Cancel was pressed on the Select Printer form. The report
will not be printed.", vbOKOnly + vbInformation, "Cancel Pressed"
End Select
' Unload the SelectPrint form
Unload SelectPrint

' Get job status information and display it
result% = PEGetJobStatus(jobnum%, JobInfo)
ErrorTrap "GetJobStatus in PrintCustom", jobnum%
' Build status string based on results from job status call
Select Case result%
Case PE_JOBNOTSTARTED
TempText$ = "Job Not Started"
Case PE_JOBINPROGRESS
TempText$ = "Job In Progress"
Case PE_JOBCOMPLETED
TempText$ = "Job Completed"
Case PE_JOBFAILED
TempText$ = "Job Failed"
Case PE_JOBCANCELLED
TempText$ = "Job Cancelled"
End Select
TempText$ = "Job Status: " & TempText$ & Chr$(10)
TempText$ = TempText$ & "Records Read: " & JobInfo.NumRecordsRead & Chr$(10)
TempText$ = TempText$ & "Records Selected: " & JobInfo.NumRecordsSelected &
Chr$(10)
TempText$ = TempText$ & "Records Printed: " & JobInfo.NumRecordsPrinted &
Chr$(10)
TempText$ = TempText$ & "Display Page Num: " & JobInfo.DisplayPageN &
Chr$(10)
TempText$ = TempText$ & "Latest Page Num: " & JobInfo.LatestPageN & Chr$(10)
TempText$ = TempText$ & "Starting Page Num: " & JobInfo.StartPageN & Chr$(10)
TempText$ = TempText$ & IIf(JobInfo.PrintEnded, "Print has ended.", "Print
has not ended.")
' Display results and stats for job
MsgBox TempText$, vbOKOnly, "Job Status"

' Subreport check - if a subreport is currently open, call PreviewReport to
offer a chance to preview the main report, then close the subreport and
main report
If lblSubreportName.Visible Then
result% = PECloseSubreport(jobnum%)
ErrorTrap "CloseSubReport in PrintCustom", mainjob%
PEClosePrintJob mainjob%
Else
PEClosePrintJob jobnum%
End If

PECloseEngine

MsgBox "Custom-Link Print Complete!", vbOKOnly, "Operation Succeeded"

End Sub
ActiveX
Private Sub mnuPrintPrint_Click()

CrystalReport1.ReportFileName = lblReportName.Caption
' Name from label on sample form

' Discard saved data?
If MsgBox("Do you wish to discard any saved data?", vbYesNo + vbQuestion,
"Discard Saved Data?") = vbYes Then
CrystalReport1.DiscardSavedData = 1
End If

' Display progress dialog?
If MsgBox("Do you want to see the progress dialog?", vbYesNo + vbQuestion,
"Display Progress Dialog?") = vbYes Then
CrystalReport1.ProgressDialog = True
Else
CrystalReport1.ProgressDialog = False
End If

CrystalReport1.Destination = 1 ' Printer

' Display Windows printer selection dialog
CrystalReport1.PrinterSelect

' Print
CrystalReport1.Action = 1

MsgBox "Print Complete!", vbOKOnly, "Operation Completed"

End Sub


Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com